<?php

/**
 * Store some special users from the front side.
 */
class AdminFrontUser extends EMongoDocument
{
    public $user_id;
    
    public function getCollectionName()
    {
        return 'admin_front_user';
    }

    /**
	 * Returns the static model of the specified AR class.
	 * @return AdminFrontUser the static model class
	 */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function indexes()
    {
        return array(
            'user_id'=>array('key'=>array('user_id'=>EMongoCriteria::SORT_ASC), 'unique'=>true),
        );
    }
    
    /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('user_id', 'required'),
            array('ctime, mtime', 'safe'),
		);
	}
    
    /**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'_id' => 'ID',
			'user_id' => 'User_id',
			'ctime' => 'Created Time',
			'mtime' => 'modified Time',
		);
	}
    
    /**
     * get the user object
     * @return User
     */
    public function getUser()
    {
        return User::model()->getById($this->user_id);
    }
    
    public function getDisplayUsername()
    {
        $user = $this->getUser();
        if($user instanceof User)
            return $user->getDisplayName();
        else
            return '';
    }
    
    /**
     * get all the user id array
     * @return array
     */
    public function getAllUserIds()
    {
        $userIds = array();
        $this->setUseCursor(true);
        $cursorMongo = $this->findAll();
        $cursor = $cursorMongo->getCursor();
        while($cursor->hasNext())
        {
            $doc = $cursor->getNext();
            $userIds[] = $doc['user_id'];
        }
        
        return $userIds;
    }
    
    /**
     * get all the user object.
     * @return array of User
     */
    public function getAllUsers()
    {
        if($userIds = $this->getAllUserIds())
            return User::model()->getByIds($userIds);
        else
            return array();
    }
}